package com.shemy.fileservice.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @Description 配置
 * @Author Dzsom
 * @Date 2018-10-19 23:14
 * @Encoding UTF-8
 * @Version 1.0
 **/
@Component
@ConfigurationProperties(prefix = "file.config")
@Getter
@Setter
public class FileConfig {
    /**
     * 路径
     */
    //保存硬盘地址
    private String hardDisk;
    //默认图片类型文件夹
    private String imageFolder;
    //默认文件类型文件夹
    private String documentFolder;
    //默认的视频类型文件夹
    private String videoFolder;
    //默认的音频类型文件夹
    private String musicFolder;
    //允许上传的ip(白名单)
    private String[] IPs;
    /**
     * 类型
     */
    //图片类型
    private String[] imageType;
    //文件类型
    private String[] documentType;
    //视频类型
    private String[] videoType;
    //音频类型
    private String[] musicType;


    public String fileType(String extName){
        String filePath;
        //图片类型文件
        if (this.inArray(this.getImageType(), extName)) {
            filePath = this.getImageFolder();
        }
        //视频类型文件
        else if (this.inArray(this.getVideoType(), extName)) {
            filePath = this.getVideoFolder();
        }
        //文档类型文件
        else if (this.inArray(this.getDocumentType(), extName)) {
            filePath = this.getDocumentFolder();
        }
        //音频类型文件
        else if (this.inArray(this.getMusicType(), extName)) {
            filePath = this.getMusicFolder();
        } else {
            filePath= "";
        }
        return filePath;
    }

    /**
     * 指定文件类型
     * @param extName 文件后缀名
     * @param type 文件类型(1.Image ,2.Video ,3.Document ,4.Music)
     * @return
     */
    public Boolean fileByType(String extName,Integer type){

        switch (type){
            case 1:
                if (this.inArray(this.getImageType(), extName)) {
                    return true;
                }
                break;
            case 2:
                if (this.inArray(this.getVideoType(), extName)) {
                    return true;
                }
                break;
            case 3:
                if (this.inArray(this.getDocumentType(), extName)) {
                    return true;
                }
                break;
            case 4:
                if (this.inArray(this.getMusicType(), extName)) {
                    return true;
                }
                break;
            default:
        }
        return false;
    }

    /**
     * 判断数组中是否包含某个元素
     * @param array 类型的数组
     * @param element 被检查的类型
     * @return
     */
    private boolean inArray(String[] array,String element) {
        boolean flag = false;
        for(String type : array) {
            if(element.equals(type)){
                flag = true;
                break;
            }
        }
        return flag;
    }
}
